<?php
class User extends Model {
  function User() {
    parent::Model();
  }

  function validateUser() {
    $this->db->where('userName', $this->input->post('username'));
    $this->db->like('password', encrypt($this->input->post('password')));
    $query = $this->db->get('user');

    if($query->num_rows() == 1) {
      $this->load->helper('date');

      $userResult = $query->row();

      $this->db->select('clubId, vip');
      $this->db->where('id',$userResult->accountId);
      $query = $this->db->get('account');

      $str = $this->db->last_query();

      $clubResult = $query->row();
      $clubId = $clubResult->clubId;
      $isVIP = (bool) $clubResult->vip;

      $this->db->set('lastLogin',date('Y-m-d H:i:s'));
      $this->db->where('accountId',$userResult->accountId);
      $this->db->update('user');

      return array('username' => $this->input->post('username'),
      'password' => encrypt($this->input->post('password')),
      'nickName' => $userResult->nickName,
      'clubId' => $clubId,
      'isVIP' => $isVIP,
      'lastLogin' => $userResult->lastLogin
      );
    } else {
      return NULL;
    }
  }

  function create($accountId) {
    $new_member_insert_data = array(
        'firstName' => $this->input->post('first_name'),
        'nickName' => $this->input->post('nick_name'),
        'lastName' => $this->input->post('last_name'),
        'emailAddress' => $this->input->post('email_address'),
        'username' => $this->input->post('username'),
        'password' => encrypt($this->input->post('password')),
        'accountId' => $accountId
    );

    $insert = $this->db->insert('user', $new_member_insert_data);
    return $insert;
  }
}
?>